#include <stdio.h>
#include <stdlib.h>
#include <clutter/clutter.h>

static gboolean threadIsRunning = FALSE;

typedef struct
{
    gint count;
    ClutterActor *progress_rect;
    ClutterActor *progress_fill;
    ClutterActor *label;
} Progress;

static gboolean update_idle(gpointer data)
{
    Progress *progress = data;
    gchar *text;
    gfloat width, height;

    clutter_actor_get_size (progress->progress_rect, &width, &height);
    text = g_strdup_printf ("Count to %d totle_width=%d", progress->count, (int)width);
    clutter_text_set_text (CLUTTER_TEXT (progress->label), text);

    clutter_actor_save_easing_state(progress->progress_fill);
    clutter_actor_set_width(progress->progress_fill, progress->count*width/100);
    clutter_actor_restore_easing_state(progress->progress_fill);

    g_free (text);
    return G_SOURCE_REMOVE;
}

static gpointer test_thread_func(gpointer user_data)
{
    gint i;
    Progress *progress = user_data;
    for (i = 0; i <= 100; i++)
    {
        if( threadIsRunning == FALSE )
        {
            g_thread_exit(0);
        }

        progress->count = i;        
        g_usleep(G_USEC_PER_SEC/10);
        clutter_threads_add_idle_full(G_PRIORITY_HIGH, update_idle, progress, NULL);
    }

    threadIsRunning = FALSE;
    return NULL;
}

static gboolean on_actor_button_press(ClutterStage *stage, ClutterEvent *event, gpointer data)
{
    Progress *progress = data;
    if( threadIsRunning == FALSE )
    {
        printf("Will Start Loading \n");
        threadIsRunning = TRUE;
        clutter_text_set_text (CLUTTER_TEXT (stage), "Stop Loading");
        clutter_actor_set_background_color (stage, CLUTTER_COLOR_Chocolate);
        g_thread_new("counter", test_thread_func, progress);
    }
    else
    {
        printf("Will Stop Loading \n");
        threadIsRunning = FALSE;
        clutter_text_set_text (CLUTTER_TEXT (stage), "Start Loading");
        clutter_actor_set_background_color (stage, CLUTTER_COLOR_Chocolate);
    }

    return TRUE;
}

int main(int argc, char *argv[])
{
    ClutterActor *stage;
    ClutterActor *button;
    Progress progress;
    if (clutter_init(&argc, &argv) != CLUTTER_INIT_SUCCESS)
        return 1;

    stage = clutter_stage_new();
    clutter_stage_set_title(CLUTTER_STAGE(stage), "Threading");
    clutter_actor_set_background_color(stage, CLUTTER_COLOR_Aluminium3);
    clutter_actor_set_size(stage, 600, 300);
    g_signal_connect(stage, "destroy", G_CALLBACK(clutter_main_quit), NULL);

    /* add button */
    button = clutter_text_new_with_text ("Mono 16", "Start Loading");
    clutter_actor_set_position(button, 50, 100);
    clutter_actor_set_size (button, 200, 30);
    clutter_actor_add_child(stage, button);
    clutter_actor_set_background_color (button, CLUTTER_COLOR_DarkBlue);
    clutter_actor_set_reactive (button, TRUE);
    g_signal_connect (button, "button-press-event", G_CALLBACK (on_actor_button_press), &progress);


    /* a progress bar */
    progress.progress_rect = clutter_actor_new();
    clutter_actor_set_background_color(progress.progress_rect, CLUTTER_COLOR_DarkChameleon);
    clutter_actor_set_position(progress.progress_rect, 50, 225);
    clutter_actor_set_size(progress.progress_rect, 350, 40);
    clutter_actor_add_child(stage, progress.progress_rect);
    progress.progress_fill = clutter_actor_new();
    clutter_actor_set_background_color(progress.progress_fill, CLUTTER_COLOR_Orange);
    clutter_actor_set_position(progress.progress_fill, 50, 225);
    clutter_actor_set_size(progress.progress_fill, 0, 40);
    clutter_actor_add_child(stage, progress.progress_fill);


    progress.label = clutter_text_new_with_text ("Mono 12", "");
    clutter_actor_set_position (progress.label, 50, 50);
    clutter_actor_add_child (stage, progress.label);


    clutter_actor_show(stage);
    clutter_main();

    return EXIT_SUCCESS;
}
